//
//  MainChartView.m
//  ChartRefactor
//
//  Created by CaoJun on 15/3/11.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "MainChartKLineView.h"

using namespace ytx;

@interface MainChartKLineView()

@end

@implementation MainChartKLineView

-(void)constructor
{
    [super constructor];
    
    [self addSubview:self.rectPainterView];
    
    [self addSubview:self.kLinePainterView];
    
    [self addSubview:self.linePainterView];
    
    [self addSubview:self.indexPainterView];
    
    [self addSubview:self.textPainterView];
    
    [self addSubview:self.crosshairsPainterView];
    
    [self addSubview:self.userPainterView];
    
    self.rectPainterView.indexPosition = self.indexPosition;
    
    self.kLinePainterView.indexPosition = self.indexPosition;
    
    self.linePainterView.indexPosition = self.indexPosition;
    
    self.indexPainterView.indexPosition = self.indexPosition;
    
    self.textPainterView.indexPosition = self.indexPosition;
    
    self.crosshairsPainterView.indexPosition = self.indexPosition;
    
    self.userPainterView.indexPosition = self.indexPosition;

}

-(void)render:(KLineData *) data
{
    if (data == NULL) {
        return;
    }
    self.kLineData = data;
    self.indexPosition->indexType = data->indexType;
    [self resize];
    self.indexPosition->initViewIndexByDataIndexAndRect(data, data->getDrawingCount(), Config::getInstance()->kLine.min, Config::getInstance()->kLine.max);
    [self paint];
}

-(void)renderKeepIndexPosition:(KLineData *) data
{
    if (data == NULL) {
        return;
    }
    self.indexPosition->indexType = data->indexType;
    if(!self.indexPosition->hadInited() || data != self.kLineData  ){
        self.indexPosition->initViewIndexByDataIndexAndRect(data, data->getDrawingCount(), Config::getInstance()->kLine.min, Config::getInstance()->kLine.max);
    }
    [self resize];
    self.indexPosition->concatCount(data);
    self.indexPosition->setPara(data);
    self.kLineData = data;
    [self paint];
}

-(void)render
{
    if (![self hasData]) {
        return;
    }
    [self resize];
    self.indexPosition->setPara(self.kLineData);
    [self paint];
}

-(void)paint{
    
    [self.rectPainterView paint: self.indexPosition withPainter: [PainterRect new] ];
    [self.kLinePainterView paint: self.indexPosition withPainter: [PainterKLine new] ];
    [self.linePainterView paint: self.indexPosition withPainter: [PainterLineIndex new] ];
    switch (self.kLineData->indexType) {
        case IndexType::TJ:
            [self.indexPainterView paint: self.indexPosition withPainter: [PainterIndexTJ new]];
            [self.textPainterView paint: self.indexPosition withPainter: [PainterTextKLine new] ];
            break;
        case IndexType::QK:
            [self.kLinePainterView clear];
            [self.linePainterView clear];
            [self.indexPainterView paint: self.indexPosition withPainter: [PainterIndexQK new]];
            [self.textPainterView paint: self.indexPosition withPainter: [PainterTextKLine new] ];
            break;
        case IndexType::QKT:
            [self.kLinePainterView clear];
            [self.linePainterView clear];
            [self.indexPainterView paint: self.indexPosition withPainter: [PainterIndexQKT new]];
            [self.textPainterView paint: self.indexPosition withPainter: [PainterTextKLine new] ];
            break;
        case IndexType::GameMatrix:
            [self.kLinePainterView clear];
            [self.linePainterView clear];
            [self.indexPainterView paint: self.indexPosition withPainter: [PainterIndexGameMatrix new]];
            [self.textPainterView paint: self.indexPosition withPainter: [PainterTextKLineGameMatrix new] ];
            break;
        case IndexType::HJJT:
            [self.kLinePainterView clear];
            [self.linePainterView clear];
            [self.indexPainterView paint: self.indexPosition withPainter: [PainterIndexHJJT new]];
            [self.textPainterView paint: self.indexPosition withPainter: [PainterTextKLine new] ];
            [self.kLinePainterView paint: self.indexPosition withPainter: [PainterKLine new] ];
            break;
        default:
            [self.textPainterView paint: self.indexPosition withPainter: [PainterTextKLine new] ];
            [self.indexPainterView clear];
//            layerIndex->setPainter(new PainterBase());
            break;
    }
    
    [self.userPainterView paint: self.indexPosition withPainter: [PainterUserKLineData new] ];
    
    [super paint];
}

- (BOOL) hasData
{
    return self.kLineData != NULL && self.kLineData->hasData();
}

- (void) clear
{
    [self.rectPainterView clear];
    [self.kLinePainterView clear];
    [self.linePainterView clear];
    [self.textPainterView clear];
    [self.indexPainterView clear];
    [self.crosshairsPainterView clear];
    [self.userPainterView clear];
    
    [super clear];
}

@end
